$(function() {
	//设置活动按钮
	changeHeaderMenu(1);
	changeMenu(1, 0);
	//初始化列表数据
	config.agentIndex = getUrlParam('agentIndex');
	if(heading.userName == "admin" && !config.agentIndex)
		$('.myType').append('<option value="3">代理商充值</option>');
	if(config.agentIndex)
		$('#form_back').show();
	var data = {};
	if(config.agentIndex)
		data.agentIndex = config.agentIndex;
	findTotal(data);
	initTable();
	savehandle();
});

var config = {};

function savehandle() {
	$('#alertdiv').hide();
	$('#alertdiv').savehandle({
		fieldMap : {},
		successMsg : "成功！",
		failMsg : "失败，请重试！",
		success : successCallBack
	});
}

function successCallBack() {
	refresh();
}

/**
 * 初始化表格数据
 */
function initTable() {
	var params = {};
	if(config.agentIndex)
		params.agentIndex = config.agentIndex;
	$('#grid').grid({
		title : '',
		pager : true,
		tbar : '#navbar',
		store : {
			url : '/fw/agent/agentCon_getlist.action',
			idProperty : 'agentIndex',
			uploadPagesize : 'rows',
			params: {
				data: params
			}
		},
		pageCfg : {
			btnCount : 5
		},
		columns : [
		        {cn:'消费类型', en:'type', width : 80, tpl: function(v) {
		        	var t = v.type;
		        	if(t == 1)
		        		return "设备续时";
		        	if(t == 3)
		        		return "代理商充值";
		        	return "充值点卡";
		        }},
		        {cn:'消费点数', en:'point', width : 80},
		        {cn:'续时时长(月)', en:'time',	width : 120},
		        {cn:'授权主机数量', en:'host',	width : 120},
		        {cn:'充值对象', en:'sn',	width : 250, tpl: function(v) {
		        	if(v.type == 3) {
		        		return "代理商：" + (v.agentName?v.agentName:'');
		        	} else {
			        	var t = v.sn;
			        	if(t == ""||t == null){
			        		if(v.cardNum == ""||v.cardNum == null){
			        			return "暂无数据";
			        		}else {
			        			return "卡号 : "+v.cardNum;
			        		}
			        	}
			        	return "设备 : "+t;
		        	}
		        }},
		        {cn:'充值结果', en:'result',width : 80, tpl: function(v) {
		        	var t = v.result;
		        	if(t == "1")
		        		return "成功";
		        	return "失败";
		        }},
		        {cn:'充值时间', en:'ctime',width : 200, tpl: function(v) {
		        	var t = v.ctime;
		        	if(t && t.length >= 14)
		        		return t.substr(0, 4) + '-' + t.substr(4, 2) + '-' + t.substr(6, 2) + ' ' + t.substr(8, 2) + ':' + t.substr(10, 2) + ':' + t.substr(12, 2);
		        	return t;
		        }},
		        {cn:'使用情况', en: 'cardIndex', width: 150, tpl: function(v) {
		        	var html = '';
		        	if(v.type == 2) {
		        		if(v.cardSta == "valid") {
		        			html += '未被使用';
		        		} else if(v.cardSta == "invalid") {
		        			html += '已被使用';
		        			html += '&nbsp;&nbsp;<button onclick="Usage(\''+v.cardNum+'\', this)" class="btn btn-success btn-xs">使用详情</button>';
		        		}
		        	}
		        	return html;
		        }}
				]
	});
}

function Usage(cardIndex, obj) {
	$.getJSON('/fw/agent/agentRec_getbycardnum.action', {
		'cardNum': cardIndex
	}, function(data){
		$('#myModal').modal('show');
		var r = data.result.rows[0];
		var time = r.rechargeTime*1000;
		var days    = time / 1000 / 60 / 60 / 24 / 31;
		days =  Math.floor(days);
		var html = '';
		html += '<div>使用者 : ' + r.sn;
		html += '<div>充值时间 : ' + days+" 月";
		html += '<div>有效主机 : ' + r.rechargeHost;
		html += '<br/>有效AC : ' + r.rechargeAc;
		html += '<br/>使用时间 : ' + r.time.split('.')[0] + '</div>';
		$('.data_card').html(html);
	});
}

function formatTime(t) {
	if(t && t.length >= 14)
		return t.substr(0, 4) + '-' + t.substr(4, 2) + '-' + t.substr(6, 2) + ' ' + t.substr(8, 2) + ':' + t.substr(10, 2) + ':' + t.substr(12, 2);
	return t;
}

function del(){
	var ids = $('#grid').grid('getSelectedIds');
	if(ids.length == 0) {
		$('#alertdiv').savehandle('setmsg',{msg:'请选择要删除的数据!',timer:2000});
		return;
	}else{
		var idsStr = ids.join(',');
		if(confirm("这些数据确定要删除数据？")){
			$.getJSON(
				'/fw/agent/agent_del.action',
				{
					'ids':idsStr
				},
				function(data){
					$('#alertdiv').savehandle('handle',data,2000);
					refresh();
				}
			);
		}
	}
}
function findType(){
	var type = $('.myType').val();
	var startDate = $('#startDate').val();
	var endDate = $('#endDate').val();
	var data = {
		agentIndex: config.agentIndex,
		type: type
	};
	if(startDate) {
		data.startDate = startDate.replace("-", "").replace("-", "").replace(" ", "").replace(":", "").replace(":", "");
	}
	if(endDate) {
		data.endDate = endDate.replace("-", "").replace("-", "").replace(" ", "").replace(":", "").replace(":", "");
	}
	findTotal(data);
	$('#grid').grid('load',{
		data: data
	});
}

function findTotal(data) {
	$.getJSON('/fw/agent/agentCon_gettotal.action', data, function(data) {
		if(data.success && data.result) {
			$('#pointTotal span').html(data.result + '点');
		} else {
			$('#pointTotal span').html('查询失败');
		}
	});
}

/**
 * 刷新
 * 
 * @return {TypeName}
 */
function refresh() {
	$('#grid').grid('load', {data : {}});
}

function findAll() {
	refresh();
}